package oa.struts.action;

import java.util.Date;

import javax.annotation.Resource;

import oa.domain.forum.Reply;
import oa.domain.forum.Topic;
import oa.service.ReplyService;
import oa.service.TopicService;
import oa.struts.action.base.BaseAction;
import oa.util.OAUtils;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

@Controller("replyAction")
public class ReplyAction extends BaseAction<Reply>{

	private Long topicId;
	
	@Resource(name="replyService")
	private ReplyService replyService;
	
	@Resource(name="topicService")
	private TopicService topicService;
	
	public Long getTopicId() {
		return topicId;
	}
	public void setTopicId(Long topicId) {
		this.topicId = topicId;
	}
	public String addUI()
	{
	    Topic topic = this.topicService.getTopicById(topicId);
	    ActionContext.getContext().getValueStack().push(topic);
		return addUI;
	}
	public String add()
	{
		Reply reply = new Reply();
		BeanUtils.copyProperties(this.getModel(), reply);
		reply.setAuthor(OAUtils.getUserFromSession());//设置回复人为当前登录用户
		reply.setDeleted(0); //默认为未删除
		reply.setIpAddress(OAUtils.getIpAddress());//设置客户端的ip地址
		reply.setPostTime(new Date());//设置回复时间为当前时间
		reply.setTopic(this.topicService.getTopicById(topicId));//回复关联主题
		this.replyService.saveReply(reply);
		return "toReplyList";
	}
	
}
